package cn.da.shuai.cool.finance.controller;

import cn.da.shuai.cool.common.entity.R;
import cn.da.shuai.cool.finance.model.CoolFinancialAnalytics;
import cn.da.shuai.cool.finance.service.ICoolFinancialAnalyticsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 财务分析控制器
 */
@RestController
@RequestMapping("/api/finance/analytics")
@Tag(name = "财务分析接口")
public class CoolFinancialAnalyticsController {

    @Autowired
    private ICoolFinancialAnalyticsService financialAnalyticsService;

    @PostMapping("/generate/monthly")
    @Operation(summary = "生成月度财务分析数据")
    public R<Void> generateMonthlyAnalytics(
            @Parameter(description = "财政年度", required = true) @RequestParam Integer fiscalYear,
            @Parameter(description = "财政月份", required = true) @RequestParam Integer fiscalMonth) {
        financialAnalyticsService.generateMonthlyAnalytics(fiscalYear, fiscalMonth);
        return R.ok();
    }

    @PostMapping("/generate/quarterly")
    @Operation(summary = "生成季度财务分析数据")
    public R<Void> generateQuarterlyAnalytics(
            @Parameter(description = "财政年度", required = true) @RequestParam Integer fiscalYear,
            @Parameter(description = "财政季度(1-4)", required = true) @RequestParam Integer fiscalQuarter) {
        financialAnalyticsService.generateQuarterlyAnalytics(fiscalYear, fiscalQuarter);
        return R.ok();
    }

    @PostMapping("/generate/yearly")
    @Operation(summary = "生成年度财务分析数据")
    public R<Void> generateYearlyAnalytics(
            @Parameter(description = "财政年度", required = true) @RequestParam Integer fiscalYear) {
        financialAnalyticsService.generateYearlyAnalytics(fiscalYear);
        return R.ok();
    }

    @GetMapping("/department/{departmentId}")
    @Operation(summary = "获取指定部门的财务分析数据")
    public R<List<CoolFinancialAnalytics>> getAnalyticsByDepartment(
            @Parameter(description = "部门ID", required = true) @PathVariable Long departmentId,
            @Parameter(description = "财政年度", required = true) @RequestParam Integer fiscalYear) {
        List<CoolFinancialAnalytics> analytics = financialAnalyticsService.getAnalyticsByDepartment(departmentId, fiscalYear);
        return R.ok(analytics);
    }

    @GetMapping("/period")
    @Operation(summary = "获取指定时期的财务分析数据")
    public R<List<CoolFinancialAnalytics>> getAnalyticsByPeriod(
            @Parameter(description = "财政年度", required = true) @RequestParam Integer fiscalYear,
            @Parameter(description = "财政月份", required = true) @RequestParam Integer fiscalMonth) {
        List<CoolFinancialAnalytics> analytics = financialAnalyticsService.getAnalyticsByPeriod(fiscalYear, fiscalMonth);
        return R.ok(analytics);
    }

    @GetMapping("/dashboard")
    @Operation(summary = "获取财务仪表盘指标")
    public R<Map<String, Object>> getDashboardMetrics(
            @Parameter(description = "财政年度", required = true) @RequestParam Integer fiscalYear) {
        Map<String, Object> dashboard = financialAnalyticsService.getDashboardMetrics(fiscalYear);
        return R.ok(dashboard);
    }

    @GetMapping("/trend")
    @Operation(summary = "获取财务指标趋势分析")
    public R<Map<String, Object>> getTrendAnalysis(
            @Parameter(description = "财政年度", required = true) @RequestParam Integer fiscalYear,
            @Parameter(description = "指标名称", required = true) @RequestParam String metricName) {
        Map<String, Object> trends = financialAnalyticsService.getTrendAnalysis(fiscalYear, metricName);
        return R.ok(trends);
    }
}